--
-- Author: shunguo.chen
-- Date: 2018-01-05 14:45:23
--
-- PvpBookView pvp规则

local NewRareInfoView = require("app.newBag.views.NewRareInfoView")
local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpBookScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
			executeFunc(self._onCloseView, self)
		end)

	self._tltText = self._ui["Panel/Panel_left/tlt"]
	self._descText = self._ui["Panel/Panel_left/Text"]
	self._icon = self._ui["Panel/Panel_left/Image"]

	-- rewards
	local itemPanel = self._ui["Panel/Panel_right/Panel_item"]
	self._rewards = {0,0,0}
	for i=1,3 do
		local con = itemPanel:getChildByName("Panel_"..i)
		self._rewards[i] = con
	end
	self._rewardText = itemPanel:getChildByName("Text_0")
	self._numText = itemPanel:getChildByName("Text_num")
	self._numText:setVisible(false)

	-- rule desc
	self._ruleText = self._ui["Panel/Panel_right/Panel_text/text"]

	-- box
	local boxPanel = self._ui["Panel/Panel_right/Panel_box"]
	self._starBar = boxPanel:findChild("bar_1/LoadingBar")

	boxPanel:findChild("Panel_icon/lock"):setVisible(false)
	boxPanel:findChild("Panel_icon/mask"):setVisible(false)

	self._boxMask = boxPanel:findChild("Panel_icon/icon_2")
	self._boxMask:setTouchEnabled(true)
	display.uiAddClick(self._boxMask, function(sender, eventType)
		local dateTemp = {
			name = L("starbox_name"),
			string1 = L("starbox_des")
		}
		self:_showRareInfo( dateTemp, self._boxMask )
	end)

	local box = boxPanel:findChild("Panel_icon/icon")
	display.uiAddClick(box, function(sender, eventType)
		executeFunc(self._onOperate, "openBox")
	end, true)

	self._boxNumCon = boxPanel:getChildByName("bg_num")
	self._boxNumText = self._boxNumCon:getChildByName("num")

end

function ClassRef:show(index)
	self:setVisible(true)
end

-- data = PvpInfo, teamTarget = 玩法目标
function ClassRef:refresh(data, teamTarget)
	self._id = teamTarget.id

	local extraReward = GD:queryChampionshipsConf( self._id )
	self._descText:setString(L(extraReward.championTips))
	self._icon:loadTexture(Res.teamtarget_pic .. extraReward.championBg .. ".png")
	self._ruleText:setString(L(extraReward.championDes))

	self._tltText:setString(teamTarget:getName())

	self:refreshPvpInfo(data)
end

function ClassRef:refreshPvpInfo(data)
	-- rewards
	local rewards = data:getRewards( self._id )
	for i,v in ipairs(rewards) do
		local con = self._rewards[i]
		local text = con:getChildByName("Text")
		if i == 1 then
			if v.isFirstWin then
				text:setString( L("tips_1150022") )
			else
				text:setString( L("tips_1150023") )
			end
		else
			text:setVisible(v.isBoost)
		end
		local render = IconRender.create(con:getChildByName("icon"))
		render:setData(v.itemData)
		render:setTipEnable(true)

		if i > 1 then
			con:getChildByName("name"):setString(v.itemData:getName())
		end
	end
	local str, colorChange = data:getNextStr(self._id)
	local color = colorChange and display.COLOR_RED or cc.c3b(255, 234, 170)
	self._rewardText:setString(str)
	self._rewardText:setTextColor(color)

	-- self._numText:setVisible(data:isBoost(self._id))
	-- self._numText:setString(data:getBoostCount(self._id))

	if self._rewardRichText then 
		self._rewardRichText:removeFromParent()
		self._rewardRichText = nil
	end

	-- 是否处在鼓励状态
	if data:isBoost(self._id) then
		self._rewardText:setString("")
		local count = data:getBoostCount(self._id)

		local defaults = {
    	    KEY_FONT_FACE = Res.font,
    	    KEY_FONT_SIZE = 18,
    	    KEY_FONT_COLOR_STRING = "#FFEAAA",
    	}
    	local str1 = display.getRichTextFontXml(count, "#9CFF00", 18)
   		local text1 = string.format(str, str1)
   		local text2 = string.format("<outline color='#000000' size='2'>%s</outline>", text1) -- 添加描边
   		-- dump(text2)
	
    	local richText = ccui.RichText:createWithXML(text2, defaults)
    	richText:ignoreContentAdaptWithSize(false)
    	richText:setContentSize( cc.size( 334, 0 ) )
    	
    	richText:setAnchorPoint(cc.p(0, 0))
    	richText:formatText()
    	richText:setPosition(cc.p(34, 20))
    	richText:addTo(self._ui["Panel_right/Panel_item"])
	
    	self._rewardRichText = richText
	end

	-- box
	local star = data:getStar()
	local boxNum = data:getBoxNum()
	local hasBox = boxNum > 0
	local maxStar = tonumber(GD:getSystemPar("starpoint", 10))
	self._starBar:setPercent((star / maxStar) * 100)
	self._boxMask:setVisible(not hasBox)
	self._boxNumCon:setVisible(hasBox)

	if hasBox then
		self._boxNumText:setString(boxNum)
		self:openAction(true)
	else
		self:pauseAction()
	end
end

function ClassRef:_showRareInfo( dataTemp, triggerNode )
	if self._rareInfoPanel then
		self._rareInfoPanel:setData( dataTemp )
	else
		self._rareInfoPanel = NewRareInfoView.create()
		self._rareInfoPanel:setData( dataTemp )
		self._ui["Panel"]:addChild( self._rareInfoPanel )

		-- 点击空白区域关闭
		display.nodeRemoveForNotHit(self._rareInfoPanel, function()
			if self._rareInfoPanel then
				self._rareInfoPanel:removeFromParent()
				self._rareInfoPanel = nil
			end
		end)
	end

	local box = triggerNode:getBoundingBox()
	local worldPos = triggerNode:getParent():convertToWorldSpace( cc.p( box.x, box.y ) )
	box.x = worldPos.x
	box.y = worldPos.y
	display.adjustNodePos( box, self._rareInfoPanel )
end

return ClassRef
